/* Copyright (c) 2025 Huawei Technologies Co., Ltd.
 * openUBMC is licensed under Mulan PSL v2.
 * You can use this software according to the terms and conditions of the Mulan PSL v2.
 * You may obtain a copy of Mulan PSL v2 at:
 *          http://license.coscl.org.cn/MulanPSL2
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PSL v2 for more details.
 */

#ifndef __L_CALLBACKS_H__
#define __L_CALLBACKS_H__

#include "luawrapper.h"
#include <mutex>
#include <thread>
#include "common.h"

using t_send_recv_cb = luawrap::lua_function<
    tuple<int32_t, optional<string_view>>(string, string_view, uint8_t, uint32_t), luawrap::ref_value>;

class LCallbacks {
public:
    struct st_cb {
        mutex                    send_recv_mutex;
        optional<t_send_recv_cb> send_recv_cb;

        st_cb() noexcept {};
    };

    static unordered_map<string, unique_ptr<st_cb>> cb_map;

    LCallbacks() {};
    ~LCallbacks() = default;

    void set_send_recv_cb(const string& device_name, optional<t_send_recv_cb> cb);
    static int32_t send_recv(const string& device_name, const uint8_t *request, size_t request_size, uint8_t *response,
        size_t *response_size);

    static void register_to(lua_State *L, luawrap::stack_table &t);
};

#endif // __L_CALLBACKS_H__